﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApDisDisassemblyDynamicRootApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApDisDisassemblyDynamicRootApp
{
  public void SetDisassemblyDynamicRoot(ITxObject selectedObj)
  {
    DialogResult dialogResult = DialogResult.Yes;
    if (this.IsSetAsAssemblyUnit(selectedObj))
    {
      dialogResult = this.ShowObjectIsAssemblyUnitWarrningMessage(selectedObj);
      if (dialogResult == DialogResult.Yes)
        this.ClearAssemblyUnit(selectedObj);
    }
    if (dialogResult != DialogResult.Yes)
      return;
    if (TxAutomaticDisassembly.DynamicRoot != null)
      dialogResult = this.ShowDynamicRootAlreadyExists();
    if (dialogResult != DialogResult.Yes)
      return;
    TxAutomaticDisassembly.DynamicRoot = selectedObj;
  }

  public static bool IsCurrentSelectionValid()
  {
    bool flag = false;
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    if (((Collection<ITxObject>) allItems).Count == 1)
    {
      ITxObject itxObject = ((Collection<ITxObject>) allItems)[0];
      if (TxAutomaticDisassembly.DynamicRoot != itxObject)
      {
        switch (itxObject)
        {
          case TxCompoundPart _:
          case TxLogicalCollection _:
            flag = true;
            if (itxObject is ITxEndItemProvider itxEndItemProvider && itxEndItemProvider.EndItemRepresentation != null)
            {
              flag = false;
              break;
            }
            break;
        }
      }
    }
    return flag;
  }

  private bool IsSetAsAssemblyUnit(ITxObject selectedObj)
  {
    return new TxAutomaticDisassemblyObject(selectedObj).AssemblyUnit;
  }

  private void ClearAssemblyUnit(ITxObject selectedObj)
  {
    new TxAutomaticDisassemblyObject(selectedObj).AssemblyUnit = false;
  }

  private DialogResult ShowObjectIsAssemblyUnitWarrningMessage(ITxObject selectedObj)
  {
    ResourceManager resourceManager = this.GetResourceManager();
    string format = resourceManager.GetString("SET_DISASSEMBLY_DYNAMIC_ROOT_ASSEMBLY_UNIT_WARING_MESSAGE");
    string str = resourceManager.GetString("SET_DISASSEMBLY_DYNAMIC_ROOT_CMD_NAME");
    return TxMessageBox.Show(string.Format(format, (object) selectedObj.Name), str, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  }

  private DialogResult ShowDynamicRootAlreadyExists()
  {
    ResourceManager resourceManager = this.GetResourceManager();
    return TxMessageBox.Show(resourceManager.GetString("SET_DISASSEMBLY_DYNAMIC_ROOT_MESSAGE"), resourceManager.GetString("SET_DISASSEMBLY_DYNAMIC_ROOT_CMD_NAME"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  }

  private ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.StringTable", this.GetType().Assembly);
  }
}
